package com.cn.transfer.jobhandler.saledelivery;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.cn.transfer.common.constant.KeyConstant;
import com.cn.transfer.common.entity.VvSaSaleDelivery;
import com.cn.transfer.common.entity.VvSaSaleDeliveryB;
import com.cn.transfer.common.entity.VvSaSaleOrder;
import com.cn.transfer.common.entity.VvSaSaleOrderB;
import com.cn.transfer.common.entity.jiandaoyun.JdyPurchaseOrder;
import com.cn.transfer.common.http.ApiClient;
import com.cn.transfer.common.utils.ConnmeUtils;
import com.cn.transfer.handle.impl.purchase.community.PurchaseCommunity;
import com.cn.transfer.mapper.VvSaSaleDeliveryBMapper;
import com.cn.transfer.mapper.VvSaSaleDeliveryMapper;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.Header;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: 向伟典
 * @date: 2023/10/7 17:42
 * @Description: 销货单数据修改
 */
@Component
public class SaledeliveryUpdateXxlJob {

    @Autowired
    private VvSaSaleDeliveryMapper vvSaSaleDeliveryMapper;
    @Autowired
    private VvSaSaleDeliveryBMapper vvSaSaleDeliveryBMapper;

    @Transactional
    @XxlJob("SaledeliveryUpdate")
    public void saledeliveryUpdate(){
        List<VvSaSaleDelivery> deliveryList=vvSaSaleDeliveryMapper.byChangedateList();
        int i = 0;
        for (VvSaSaleDelivery vvSaSaleDelivery : deliveryList) {
            if (vvSaSaleDeliveryMapper.inquireIsExist(vvSaSaleDelivery.getDocumentcode())==0){
                continue;
            }
            VvSaSaleDelivery saleDelivery = vvSaSaleDeliveryMapper.byOrderDocumentnumber(vvSaSaleDelivery.getSaledeliveryid());   // 表单
            boolean b = ConnmeUtils.deepEquals(vvSaSaleDelivery, saleDelivery);
            if (!b) {
                if (vvSaSaleDeliveryMapper.inquireIsExist(vvSaSaleDelivery.getDocumentcode())>0){
                    // 不一致同步到视图和简道云
                    vvSaSaleDeliveryMapper.deleteId(String.valueOf(vvSaSaleDelivery.getSaledeliveryid()));
                    vvSaSaleDeliveryBMapper.deleteDetails(String.valueOf(vvSaSaleDelivery.getSaledeliveryid()));
                }
                Header[] headers = ConnmeUtils.createHeaders();
                Map<String, Object> parameterDelete = new HashMap<>();
                parameterDelete.put("app_id", "6500165a60a971000ab0ad28");
                parameterDelete.put("entry_id", "6520efcb35a3c4a7355b275d");
                List<JdyPurchaseOrder> jdyPurchaseOrders = PurchaseCommunity.queryBased(parameterDelete, headers, vvSaSaleDelivery.getDocumentcode(),"documentcode");
                if (jdyPurchaseOrders.size() > 0) {
                    vvSaSaleDeliveryMapper.insertVvSaSaleDelivery(vvSaSaleDelivery);
                    for (VvSaSaleDeliveryB vvSaSaleDeliveryB : vvSaSaleDelivery.getVvsasaledeliveryblist()) {
                        vvSaSaleDeliveryBMapper.insertVvSaSaleDeliveryB(vvSaSaleDeliveryB);
                    }
                    Map<String, Object> parameterInster = new HashMap<>();
                    parameterInster.put("app_id", "6500165a60a971000ab0ad28");
                    parameterInster.put("entry_id", "6520efcb35a3c4a7355b275d");
                    parameterInster.put("data_id", jdyPurchaseOrders.get(0).get_id());
                    parameterInster.put("data",ConnmeUtils.convertEntityToMap(vvSaSaleDelivery));
                    String jsonSale = JSONUtil.toJsonStr(parameterInster);
                    ApiClient.sendPostRequest(KeyConstant.Jian_URL + "/v5/app/entry/data/update", headers, jsonSale);
                    i++;
                }
            }
        }
    }

}
